package com.fz.service.merchant.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.ResponseEntity;
import com.fz.dto.merchant.merUser.MerUserAdd;
import com.fz.dto.merchant.merUser.MerUserQuery;
import com.fz.dto.merchant.merUser.MerUserUpd;
import com.fz.entity.merchant.MerMenu;
import com.fz.entity.merchant.MerUser;
import com.fz.mapper.merchant.MerUserMapper;
import com.fz.service.merchant.MerUserService;
import com.fz.utils.JacksonUtils;
import com.fz.utils.StringUtils;
import com.fz.vo.merchant.merUser.MerUserVo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("unchecked")
public class MerUserServiceImpl implements MerUserService {

    @Resource
    private MerUserMapper merUserMapper;

    /**
     * @方法描述: 根据登录名查询用户信息
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/17
     */
    public MerUser findMerUserByLoginid(String loginid) {
        LambdaQueryWrapper<MerUser> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(MerUser::getLoginId, loginid);
        return merUserMapper.selectOne(lambdaQueryWrapper);
    }


    /**
     * @方法描述: 查询用户所拥有的菜单权限，并返回  GrantedAuthority
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/17
     */
    public Collection<GrantedAuthority> mapToRoleAuthoritiesMenu(String userId) {
        List<MerMenu> menus = merUserMapper.findMerMenuPermission(userId);
        Set<String> permissions = menus.stream().map(MerMenu::getPermission).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
        return permissions.stream().map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }


    @Override
    public ResponseEntity<MerUserVo> findMerUserList(Integer pageSize, Integer pageNum, MerUserQuery merUserQuery) throws Exception{
        Page<MerUserVo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<MerUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotNull(merUserQuery.getDeptId()) , MerUser::getDeptId, merUserQuery.getDeptId())
                .like(StringUtils.isNotEmpty(merUserQuery.getEmail()), MerUser::getEmail, merUserQuery.getEmail())
                .like(StringUtils.isNotEmpty(merUserQuery.getPhone()), MerUser::getPhone, merUserQuery.getPhone())
                .eq(ObjectUtil.isNotNull(merUserQuery.getStatus()), MerUser::getStatus, merUserQuery.getStatus())
                .eq(StringUtils.isNotEmpty(merUserQuery.getSex()), MerUser::getSex, merUserQuery.getSex())
                .orderByDesc(MerUser::getCreateTime);
        Page<MerUserVo> loginLogList = merUserMapper.findMerUserList(page, lambdaQueryWrapper);
        return ResponseEntity.buildSuccess(loginLogList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity updateMerUser(MerUserUpd merUserUpd) throws Exception {
        MerUser user = JacksonUtils.obj2pojo(merUserUpd, MerUser.class);
        merUserMapper.updateById(user);
        return ResponseEntity.buildSuccess();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity saveMerUser(MerUserAdd merUserAdd) throws Exception{
        MerUser user = JacksonUtils.obj2pojo(merUserAdd, MerUser.class);
        merUserMapper.insert(user);
        return ResponseEntity.buildSuccess();
    }

    @Override
    public ResponseEntity delMerUser(String ids) throws Exception {
        Integer[] integers = Convert.toIntArray(ids);
        merUserMapper.deleteBatchIds(Arrays.asList(integers));
        return ResponseEntity.buildSuccess();
    }

    @Override
    public ResponseEntity<MerUserVo> selectMerUsersList(MerUserQuery merUserQuery) throws Exception {
        LambdaQueryWrapper<MerUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotNull(merUserQuery.getDeptId()) , MerUser::getDeptId, merUserQuery.getDeptId())
                .like(StringUtils.isNotEmpty(merUserQuery.getEmail()), MerUser::getEmail, merUserQuery.getEmail())
                .like(StringUtils.isNotEmpty(merUserQuery.getPhone()), MerUser::getPhone, merUserQuery.getPhone())
                .eq(ObjectUtil.isNotNull(merUserQuery.getStatus()), MerUser::getStatus, merUserQuery.getStatus())
                .eq(StringUtils.isNotEmpty(merUserQuery.getSex()), MerUser::getSex, merUserQuery.getSex())
                .orderByDesc(MerUser::getCreateTime);
        return ResponseEntity.buildSuccess(merUserMapper.selectMerUsersList(lambdaQueryWrapper));
    }
}
